package com.david.func_test.activity

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.david.func_test.R

/**
 * 结合蓝牙beacon可实现室内导航，从后台获取beacon指向的方向（0-360度）对比根据指南针的指向（0-360度）实现导航
 */
class CompassActivity : AppCompatActivity(), SensorEventListener {
    private var mSensorManager: SensorManager? = null
    private var mAccelerometer: Sensor? = null
    private var mMagnetometer: Sensor? = null
    private val mLastAccelerometer = FloatArray(3)
    private val mLastMagnetometer = FloatArray(3)
    private var mLastAccelerometerSet = false
    private var mLastMagnetometerSet = false
    private val mR = FloatArray(9)
    private val mOrientation = FloatArray(3)
    private var mCurrentDegree = 0f
    private var mPointer: ImageView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_compass)
        mSensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
        mAccelerometer = mSensorManager!!.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
        mMagnetometer = mSensorManager!!.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)
        mPointer = findViewById<View>(R.id.pointer) as ImageView
    }

    override fun onResume() {
        super.onResume()
        mSensorManager!!.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI)
        mSensorManager!!.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_UI)
    }

    override fun onPause() {
        super.onPause()
        mSensorManager!!.unregisterListener(this, mAccelerometer)
        mSensorManager!!.unregisterListener(this, mMagnetometer)
    }

    override fun onSensorChanged(event: SensorEvent) {
        if (event.sensor == mAccelerometer) {
            System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.size)
            mLastAccelerometerSet = true
        } else if (event.sensor == mMagnetometer) {
            System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.size)
            mLastMagnetometerSet = true
        }
        if (mLastAccelerometerSet && mLastMagnetometerSet) {
            SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer)
            SensorManager.getOrientation(mR, mOrientation)
            val azimuthInRadians = mOrientation[0]//从北 到西 到南值为：0，-Math.PI/2, -Math.PI;从北 到东 到南值为：0，Math.PI/2，Math.PI
            val azimuthInDegrees =
                (Math.toDegrees(azimuthInRadians.toDouble()) + 360).toFloat() % 360
//            val ra = RotateAnimation(
//                mCurrentDegree,
//                -azimuthInDegrees,
//                Animation.RELATIVE_TO_SELF, 0.5f,
//                Animation.RELATIVE_TO_SELF,
//                0.5f
//            )
//            ra.duration = 250
//            ra.fillAfter = true
//            mPointer!!.startAnimation(ra)

            val rotate = azimuthInRadians + Math.PI//以正南为0/360度，正西为90度，正北为180度，正东为270度，
            mPointer?.rotation = -(rotate/(2 * Math.PI) * 360).toFloat()
            mCurrentDegree = -azimuthInDegrees
            Log.e("compass", "mPointer?.rotation:${mPointer?.rotation},degree:$azimuthInDegrees, azimuthInRadians: ${Math.toDegrees(azimuthInRadians.toDouble()) + 360}，azimuthInRadians：$azimuthInRadians")
        }
    }

    override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}
}